package cn.hxzy.course.service.impl;

import cn.hxzy.common.domain.Task;
import cn.hxzy.common.domain.TaskManager;
import cn.hxzy.common.domain.User;
import cn.hxzy.common.domain.vo.TaskVo;
import cn.hxzy.common.util.ThreadLocalUtil;
import cn.hxzy.course.mapper.TaskMapper;
import cn.hxzy.course.service.TaskService;
import cn.hxzy.user.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author niuyi
 * @since 2024-06-22
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Task> selectList(int taskId) {
        return null;
    }

    @Override
    public List<Task> selectTask(String taskName, Integer courseId) {
        List<Task> tasks = taskMapper.selectTask(taskName,courseId);
        if (tasks != null){
            return tasks;
        }
        return null;
    }

    @Override
    public double selectCount(Long courseId) {
        double num = taskMapper.selectCount(courseId);
        return num;
    }

    @Override
    public Task getList(int sectionId) {
        Task list = taskMapper.getList(sectionId);
        if (list != null){
            return list;
        }
        return null;
    }

    @Override
    public List<Task> selectTaskList(Integer sectionId) {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("section_id",sectionId);
        List<Task> tasks = taskMapper.selectList(queryWrapper);
        if (tasks != null){
            return tasks;
        }
        return null;
    }

    @Override
    public int deleteTask(Integer taskId) {
        int i = taskMapper.deleteById(taskId);
        return i;
    }

    //查询作业详情和小结名称
    @Override
    public Map selectTaskById(Integer taskId) {
        Map map = taskMapper.selectTaskById(taskId);
        //提取出来数据库传过来的时间转格式为yyyy-MM-dd
        if (map != null && map.containsKey("task_item")) {
            // 获取时间字段
            Date taskTime = (Date) map.get("task_item");

            // 格式化时间为年月日
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedTaskTime = sdf.format(taskTime);

            // 存回 task_item
            map.put("task_item", formattedTaskTime);
        }
        return map;
    }

    //学生上传作业
    @Override
    public int insertTask(Map map) {
        TaskManager taskManager = new TaskManager();
        //获取用户id
        Long userId = ThreadLocalUtil.getUser().getUserId();
        int i1 = userId.intValue();
        Integer stu = Integer.valueOf(i1);
        taskManager.setStudentId(stu);
        //System.out.println(map);
        //存放作业id
        taskManager.setTaskId(Integer.valueOf(map.get("taskId").toString()));
        //存放作业内容
        taskManager.setTaskTxt(map.get("answer").toString());
        //存放提交状态
        taskManager.setCommit(Integer.valueOf(map.get("commit").toString()));
        //获取当前系统时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        taskManager.setSubmitTime(LocalDateTime.parse(formattedDateTime, formatter));
        int i = taskMapper.insertTask(taskManager);
        return i;
    }

    //学生修改作业
    @Override
    public int updateTaskManger(Map map) {
        String answer = map.get("answer").toString();
        Integer studentId = (Integer) map.get("studentId");
        Integer taskId = (Integer) map.get("taskId");
        return taskMapper.updateTask(answer,studentId,taskId);
    }


    //老师创建作业
    @Override
    public int teacherInsertTask(Task task) {
        return taskMapper.teacherInsertTask(task);
    }

    //老师修改作业
    @Override
    public int teacherUpdateTask(Task task) {
        return taskMapper.teacherUpdateTask(task);
    }


    //根据小节id查询章节名称
    @Override
    public String selectSetionName(Integer id) {
        return taskMapper.selectSetionName(id);
    }

}
